const { Product } = require('@/models/product');
const getProducts = async (req, res) => {
  try {
    const { parent_id, offset, limit } = req.query;
    const { rows, total } = await Product.getProducts(parent_id, offset, limit);
    res.json({ rows, total });
  } catch (error) {
    res.status(500).json({ message: error.message });
  }
}
const createProduct = async (req, res) => {
  try {
    const data = req.body;
    const product = await Product.createProduct(data);
    res.json(product);
  } catch (error) {
    res.status(500).json({ message: error.message });
  }
}
const updateProduct = async (req, res) => {
  try {
    const { id } = req.params;
    const data = req.body;
    const product = await Product.updateProduct(id, data);
    res.json(product);
  } catch (error) {
    res.status(500).json({ message: error.message });
  }
}
const deleteProduct = async (req, res) => {
  try {
    const { id } = req.params;
    const product = await Product.deleteProduct(id);
    res.json(product);
  } catch (error) {
    res.status(500).json({ message: error.message });
  }
}
const updateStatus = async (req, res) => {
  try {
    const { id } = req.params;
    const data = req.body;
    const product = await Product.updateStatus(id, data);
    res.json(product);
  } catch (error) {
    res.status(500).json({ message: error.message });
  }
}
const getAnalytics = async (req, res) => {
  try {
    const { parent_id } = req.query;
    const analytics = await Product.getAnalytics(parent_id);
    res.json(analytics);
  } catch (error) {
    res.status(500).json({ message: error.message });
  }
}
module.exports = {
  getProducts,
  createProduct,
  updateProduct,
  deleteProduct,
  updateStatus,
  getAnalytics
}
